<?php
/*************************************************************************
Search module
----------------------------------------------------------------
Derasoft CMS Project
Company: Derasoft Co., Ltd                                  
Name: Tran Thi Kim Que                               
Last updated: 26/12/2008
**************************************************************************/
include_once(ROOT_PATH."classes/dao/procategories.class.php");
include_once(ROOT_PATH."classes/dao/products.class.php");
include_once(ROOT_PATH.'includes/functions.php');
$procategories = new ProductCategories();
$products = new Products();
$infoText = '';
$infoClass = 'infoText';
$items_per_pages = $siteConfigs->getProperty('products_per_page');;
$templateFile = "search.tpl.html";
$keyword = $request->element('keywords','');
$str = mysql_real_escape_string($keyword);
$pid= $request->element('chuyenmuc','-1');
$pid = intval($pid);
$giatu= $request->element('giatu','0');
$giatu = intval($giatu);
$giaden = $request->element('giaden','');
$giaden = intval($giaden);
$giamgia = $request->element('giamgia');
$khuyenmai = $request->element('khuyenmai');
$page = $request->element('page','1');
$lang = $request->element('lang','vn');
$template->assign('pid',$pid);
$template->assign('giamgia',$giamgia);
$template->assign('giatu',$giatu);
$template->assign('giaden',$giaden);
$template->assign('keywords',$keyword);
$template->assign('chuyenmuc',$pid);
# Build search condition
$searchCond = "status =1"; # For other cases, you can set this to "(1>0)"
if($keyword!="Nhập vào từ khóa" && $lang =='vn'){
	 $searchCond .= " AND (status =1 AND vn_name LIKE '%".$str."%' OR vn_details LIKE '%".$str."%' OR en_name LIKE '%".$str."%')";
}

if($pid!=-1) {
	$strSubCategories = $procategories->getSubCategory1($pid);
	$searchCond .= " AND category IN(".$strSubCategories.")";
}
if($giatu!=-1) $searchCond .= " AND (price >=$giatu)";
if($giaden>0) $searchCond .= " AND (price <$giaden)";
if($giamgia) $searchCond .= " AND price > s_price AND s_price > 0";
# Get all entries object and pass to template
$products_per_page = $siteConfigs->getProperty('products_per_page');
$proItems = $products->getObjects($page, $searchCond, array('date_published'=>'DESC'),$items_per_pages);
$template->assign("proItems",$proItems);
# Get number of rows, pagesa
$pages = $products->getNumItems('id', $searchCond,$items_per_pages);
$rows = $pages['rows'];
$pages = $pages['pages'];
if($page > $pages) $page = $pages;

# Generate and pass link pager to template
$listPage = linkPager1_en(ROOTPATH."/index.php?op=search&amp;keywords=".$keyword."&amp;chuyenmuc=".$pid."&amp;giatu=".$giatu."&amp;giaden=".$giaden."&amp;lang=$lang&amp;page=%s",$pages,$page);
$template->assign("pager",$listPage);
$pageName = $messages['search'];
$template->assign('pageName',$pageName);

# Pass some variables to template
$template->assign("keyword",$keyword);
$template->assign("rows",$rows);
$template->assign("page",$page);
$template->assign("pages",$pages);
#supprot
#end product
// Doan code chung 
include_once(ROOT_PATH."modules/framework.module.php");
 
# active
$active='sanpham';
$template->assign('active',$active);
?>